package com.institute.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.ExtjsAjaxResponse;
import com.common.util.ExtjsPageable;
import com.institute.entity.Institute;
import com.institute.entity.Position;
import com.institute.entity.dto.PositionDTO;
import com.institute.service.IInstituteService;
import com.institute.service.IPositionService;
import com.institute.service.InstituteService;

@Controller
@RequestMapping("/position")
public class PositionController {
	@Autowired
	private IPositionService positionService;
	@Autowired
	private IInstituteService InstituteService;
	
	@RequestMapping("/findAll")
	public @ResponseBody List<PositionDTO> findAll(){
		List<Position> positions = positionService.findAll();
		List<PositionDTO> list = new ArrayList();
		for(Position position:positions) {
			String deptName = null;
			if(positionService.getDept(position)!=null)deptName=positionService.getDept(position).getName();
			list.add(new PositionDTO(position, deptName,
					positionService.getStaffNum(position.getId())));
		}
		return list;
	}
	@RequestMapping("/findPositionPage")
	public @ResponseBody Page<PositionDTO> findPage(PositionDTO posotionDTO,Long deptId,ExtjsPageable pageable){
		Institute dept = null;
		if(deptId!=null)dept = InstituteService.findOne(deptId);
		return positionService.findAll(posotionDTO.getSpecification(posotionDTO,dept), pageable.getPageable());
	}
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtjsAjaxResponse save(PositionDTO dto) {
		try {
			if(positionService.isNameExist(dto))
				return new ExtjsAjaxResponse(false,"职务名称已存在！");
			if(positionService.isCodeExist(dto))
				return new ExtjsAjaxResponse(false,"职务编号已存在！");
			positionService.saveOrUpdate(dto);
			return new ExtjsAjaxResponse(true,"操作成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtjsAjaxResponse delete(PositionDTO position) {
		try {
			if(positionService.hasStaffs(position.getId()))return new ExtjsAjaxResponse(true,"请先移除职务在职员工！");
			positionService.delete(position.getId());
			return new ExtjsAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
		
	}
	@RequestMapping("/deletePositions")
	public @ResponseBody ExtjsAjaxResponse deletePositions(Long[] ids) {
		try {
			for(Long id:ids) {
				if(positionService.hasStaffs(id))return new ExtjsAjaxResponse(true,"请先移除职务【"+positionService.findOne(id).getName()+"】的在职员工！");
			}
			for(Long id:ids) {
				positionService.delete(id);
			}
			return new ExtjsAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(false,"操作失败！");
		}
		
	}
	@RequestMapping("/findByDeptName")
	public @ResponseBody List<PositionDTO> findByDeptName(String department){
		
		return positionService.findByDeptName(department);
		
	}
	@RequestMapping("/saveTest")
	public @ResponseBody ExtjsAjaxResponse saveTest(PositionDTO dto) {
		try {
			if(dto.getId()==0) {
				dto.setId(null);
				positionService.saveOrUpdate(dto);
				return new ExtjsAjaxResponse(true,"操作成功！");
			}else {
				positionService.saveOrUpdate(dto);
				return new ExtjsAjaxResponse(true,"操作成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(true,"操作成功！");
		}
		
	}
	@RequestMapping("/setDept")
	public @ResponseBody ExtjsAjaxResponse setDept(Long id,Long deptId) {
		try {
			positionService.changeDept(id, deptId);
			return new ExtjsAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ExtjsAjaxResponse(true,"操作失败！");
		}
	}
}
